package es.ats.codington.festival.daos;


import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;

import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAO;
import es.ats.codington.festival.mappers.EventMapper;
import es.ats.codington.festival.mappers.EventSignUpMapper;

/**
 * 
 * @author Codington_RED
 *
 */

public class EventSignUpDAOImpl implements IEventSignUpDAO {
	
	private JdbcTemplate jdbcTemplateObject;
	private Properties queryProperties;
	
	/**
	 * Default Constructor for EventSignUpDAOImpl
	 * @param conn
	 */
	
	public EventSignUpDAOImpl(JdbcTemplate jdbcTemplateObject, Properties queryProperties) {
		
		this.jdbcTemplateObject = jdbcTemplateObject;
		this.queryProperties = queryProperties;
	}
	
	
	/**
	 * let the visitor check the events signed up
	 * @param eventSignUp
	 * @return check
	 * @throws DAOException
	 */
	@Override
	public boolean checkEventsofVisitor(final EventSignUp eventSignUp) throws DAOException {
		boolean checked = false;
		
		List<EventSignUp> eventsignups = jdbcTemplateObject.query(queryProperties.getProperty("checkEvent"), 
				new PreparedStatementSetter(){

					@Override
					public void setValues(PreparedStatement ps)
							throws SQLException {
						
						ps.setInt(1, eventSignUp.getIdEvent());
						ps.setInt(2, eventSignUp.getIdVisitor());
						
					}
			
		}, new EventSignUpMapper());
		
		
		
		if(eventsignups.size() == 1){
			
			checked = true;
		}
		
		return checked;		
	}
	
	/**
	 * let the availability to the Visitor to unregister from an event
	 * @param eventSignUp
	 * @return deleteVisitorToEvent
	 * @throws DAOException
	 */
	@Override
	public boolean unregisterVisitorToEvent(final EventSignUp eventSignUp) throws DAOException {
		
		boolean deleteVisitorToEvent = false;
		Integer affectedRows = 0;
		
		affectedRows = jdbcTemplateObject.update(queryProperties.getProperty("unRegisterVisitorToEvent"), 
				new PreparedStatementSetter(){

					@Override
					public void setValues(PreparedStatement ps)
							throws SQLException {
						ps.setInt(1, eventSignUp.getIdEvent());
						ps.setInt(2, eventSignUp.getIdVisitor());
						
					}
		});
		
		if (affectedRows > 0) {
			deleteVisitorToEvent = true;
		}
		
		return deleteVisitorToEvent;
		
	}
		
	/**
	 * events which the visitors are registered in or wants to do it.
	 * @param eventsignup
	 * @return insertVisitorToEvent
	 * @throws DAOException
	 */
	@Override
	public boolean registerVisitorToEvent(final EventSignUp eventsignup) throws DAOException {
		
		boolean insertVisitorToEvent = false;
		Integer affectedRows = 0;
		
		affectedRows = jdbcTemplateObject.update(queryProperties.getProperty("registerVisitorToEvent"), 
				new PreparedStatementSetter(){

					@Override
					public void setValues(PreparedStatement ps)
							throws SQLException {
						ps.setInt(1, eventsignup.getIdEvent());
						ps.setInt(2, eventsignup.getIdVisitor());
						
					}
		});
		
		if (affectedRows > 0) {
			insertVisitorToEvent = true;
		}
		
		return insertVisitorToEvent;
	}
	
	/**
	 * events which the visitors are registered in.
	 * @param eventSignUp
	 * @return events
	 * @throws DAOException
	 */
	@Override
	public List<Event> registeredEvents(final EventSignUp eventSignUp) throws DAOException {
		
		List<Event> events = jdbcTemplateObject.query(queryProperties.getProperty("eventsRegistered"), 
				new PreparedStatementSetter(){

					@Override
					public void setValues(PreparedStatement ps)
							throws SQLException {
						
						ps.setInt(1, eventSignUp.getIdVisitor());
						
					}
			
		}, new EventMapper());
		
		return events;
		
		
	}
			
}
	
